<?php

namespace App\Http\Controllers\Client\Course;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CourseVipController extends ClientController
{

    /**
     * 会员卡详情
     */
    public function index()
    {

        $we_id = $this->we_id;

        try {
            // 查询会员到期内容
            $vip = DB::table('laifei_vip')
                ->where('we_id', '=', $we_id)
                ->where('end_time', '>', date('Y-m-d H:i:s'))
                ->orderByDesc('vip_id')
                ->first();
            $vip->end_date = date('Y年m月d日',strtotime($vip->end_time));

            // 顾问
            $adviser = DB::table('laifei_adviser_user as lau')
                ->leftJoin('laifei_sys_adviser as lsa','lau.ad_id','=','lsa.ad_id')
                ->where('lau.we_id', '=', $we_id)
                ->where('lau.is_delete', '=', 0)
                ->orderByDesc('lau.adv_id')
                ->select('lau.adv_id','lau.ad_id','lau.created_at',
                    'lsa.name','lsa.position','lsa.service_count','lsa.praise_rate','lsa.head_img','lsa.qr_code')
                ->get()
                ->map(function($item){
                    $item->head_img = AliOss::getUrl($item->head_img);
                    $item->qr_code = AliOss::getUrl($item->qr_code);
                    return $item;
                })
                ->first();

            return $this->setReturnJson(0, 'success', ['vip' => $vip, 'adviser' => $adviser]);
        } catch (\Exception $e) {
            Log::error(CourseVipController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '会员信息查询失败');
        }


    }

}
